package com.example.easeimpro.model.bean;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;


import com.example.easeimpro.model.dao.UserAccountDao;
import com.example.easeimpro.model.db.DBManager;
import com.example.easeimpro.model.db.EventListener;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2016/9/23.
 */
// 数据模型层全局类
public class Model {
    private Context mContext;
    private ExecutorService executors = Executors.newCachedThreadPool();
    private static Handler handler = new Handler(Looper.getMainLooper());
    // 创建对象
    private static Model model = null;
    private DBManager dbManager;
    //    private UserAccountDao userAccountDao;
  //  private DBManager dbManager;

    // 私有化构造
    private Model() {

    }

    // 获取单例对象
    public static Model getInstance(){
        if(model==null){
            model = new Model();
        }
            return model;

    }

    private static final String TAG = "Model";
    // 初始化的方法
    public void init(Context context){
        mContext = context;

        // 创建用户账号数据库的操作类对象
      //  userAccountDao = new UserAccountDao(mContext);

        // 开启全局监听
        EventListener eventListener = new EventListener(mContext);
    }

    // 获取全局线程池对象
    public ExecutorService getGlobalThreadPool(){
        return executors;
    }

    public UserAccountDao getAccountDbDao(Context context){
        return new UserAccountDao(context);
    }
//登录成功后处理的方法
    public void LoginSuccess(UserInfo account){
        //校验
        if(account == null){
            return;
        }

        if(dbManager != null){
            dbManager.close();
            Log.e(TAG, "LoginSuccess: "+dbManager );
        }

        dbManager = new DBManager(mContext,account.getName()+".db");
    }

    public DBManager getDbManager(){
        return dbManager;
    }

    public static Handler postHandler(){

        return handler ;
    }

}
